package net.begincode.service.user.impl;

import java.util.ArrayList;
import java.util.List;

import config.DbTemplate;
import net.begincode.bean.BcUser;
import net.begincode.service.user.BcUserService;

public class BcUserServiceImpl implements BcUserService {

	private BcUserServiceImpl() {
	}

	private static BcUserServiceImpl instance = null;

	public static BcUserServiceImpl getInstance() {
		if (instance == null) {
			synchronized (BcUserServiceImpl.class) {
				if (instance == null) {
					instance = new BcUserServiceImpl();
				}
			}
		}
		return instance;
	}

	@Override
	public int addBcUser(BcUser bcUser) {
		
		String addSql = " INSERT INTO bc_user (email, pwd, nickname, cdate,sex,tel_phone,receive_email,pic,account_power,check_flag,beginid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
		int returnValue =  DbTemplate.insert(
				addSql,
				new Object[] { bcUser.getEmail(), bcUser.getPwd(),
						bcUser.getNickname(), bcUser.getCdate(),
						bcUser.getSex(), bcUser.getTel_phone(),
						bcUser.getReceive_email(), bcUser.getPic(),
						bcUser.getAccount_power(), bcUser.getCheck_flag(),bcUser.getBeginid() });
		if(returnValue > 0){
			String sql = " select bc_user_id from bc_user where beginid = ? ";
			return DbTemplate.queryForObject(sql, new Object[]{bcUser.getBeginid()}, BcUser.class).getBc_user_id();
		}else{
			return 0;
		}
	}

	@Override
	public int updBcUser(BcUser bcUser) {
		
		List paramList = new ArrayList();
		StringBuilder updSql = new StringBuilder("update bc_user set email = ? ");
		paramList.add(bcUser.getEmail());
		if(bcUser.getPwd() != null){
			updSql.append(" ,pwd = ? ");
			paramList.add(bcUser.getPwd());
		}
		if(bcUser.getNickname() != null){
			updSql.append(" ,nickname = ? ");
			paramList.add(bcUser.getNickname());
		}
		if(bcUser.getSex() != null){
			updSql.append(" ,sex = ? ");
			paramList.add(bcUser.getSex());
		}
		if(bcUser.getTel_phone() != null){
			updSql.append(" ,tel_phone = ?");
			paramList.add(bcUser.getTel_phone());
		}
		if(bcUser.getReceive_email() != null){
			updSql.append(" , receive_email = ? ");
			paramList.add(bcUser.getReceive_email());
		}
		if(bcUser.getPic() != null){
			updSql.append(" ,pic = ? ");
			paramList.add(bcUser.getPic());
		}
		if(bcUser.getAccount_power() != null){
			updSql.append(" , account_power = ? ");
			paramList.add(bcUser.getAccount_power());
		}
		if(bcUser.getCheck_flag() != null){
			updSql.append(" , check_flag = ? ");
			paramList.add(bcUser.getCheck_flag());
		}
		updSql.append(" where bc_user_id  = ? ");
		paramList.add(bcUser.getBc_user_id());
		return DbTemplate.update(updSql.toString(), paramList.toArray());
	}

	@Override
	public List<BcUser> selUnCheckUser() {
		
		String selSql = " select * from bc_user where check_flag = 0";
		return DbTemplate.queryForListNoParam(selSql, BcUser.class);
	}

	@Override
	public BcUser selUserByEmail(String email) {
		
		String selSql = " select * from bc_user where email = ? ";
		return DbTemplate.queryForObject(selSql, new Object[]{email}, BcUser.class);
	}

	@Override
	public List<BcUser> checkEmailAndNickName(String email, String nickName) {
		
		String checkSql = " select distinct email,nickname from bc_user where email = ? or nickname = ? ";
		return DbTemplate.queryForList(checkSql, new Object[]{email,nickName},BcUser.class) ;
	}

	@Override
	public BcUser selUserByBeginId(String beginId) {
		
		String selSql = " select * from bc_user where beginid = ? ";
		return DbTemplate.queryForObject(selSql, new Object[]{beginId}, BcUser.class);

	}

	@Override
	public BcUser selUserById(int userId) {
		// TODO Auto-generated method stub
		String selSql = " select * from bc_user where bc_user_id = ? ";
		return DbTemplate.queryForObject(selSql, new Object[]{userId}, BcUser.class);
	}

}
